require 'rubygems'
require 'active_record'

module DistributedWorker
  
  module Data
    
    # == Overview
    # Datamodels for all stored data objects.
    # TODO: should client & server have the same schema?
    module Model
      
      class Application < ActiveRecord::Base
        has_many :datasets
 				belongs_to :server
      end
      
      class Dataset < ActiveRecord::Base
        belongs_to :application
      end
      
      class Client < ActiveRecord::Base
      end
      
      class Dataresult < ActiveRecord::Base
      end
      
      class Task < ActiveRecord::Base
      end
      
      class Server < ActiveRecord::Base
				has_many :applications
      end
      
    end
    
    module Migration
      
      class ApplicationTable < ActiveRecord::Migration
        
        def self.up
          create_table :applications do |t|
						t.column :uuid, :string
            t.column :server_id, :integer
						t.column :conf_path, :string
						t.column :controller_path, :string
          end
        end
        
        def self.down
          drop_table :applications
        end
        
      end
      
      class DatasetTable < ActiveRecord::Migration
        
        def self.up
          create_table :datasets do |t|
						t.column :application_id, :integer
						t.column :application_data_id, :integer
          end
        end
        
        def self.down
          drop_table :datasets
        end
        
      end
      
      class ClientTable < ActiveRecord::Migration
        
        def self.up
          create_table :clients do |t|
            t.column :uuid, :string
            t.column :ip, :string
          end
        end
        
        def self.down
          drop_table :clients
        end
        
      end
      
      class DataresultTable < ActiveRecord::Migration
        
        def self.up
          create_table :dataresults do |t|
            t.column :client_id, :integer
            t.column :dataset_id, :integer
            t.column :result, :string # TODO: should be binary data
          end
        end
        
        def self.down
          drop_table :dataresults
        end
        
      end
      
      class TaskTable < ActiveRecord::Migration
        
        def self.up
          create_table :tasks do |t|
						t.column :dataset_id, :integer
						t.column :client_id, :integer
						t.column :started_at, :timestamp
          end
        end
        
        def self.down
          drop_table :tasks
        end
        
      end
      
      class ServerTable < ActiveRecord::Migration
        
        def self.up
          create_table :servers do |t|
						t.column :uuid, :string
						t.column :ip, :string
          end
        end
        
        def self.down
          drop_table :servers
        end
        
      end
      
    end
    
  end
  
end
